package com.my.note.service;

import com.my.note.pojo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author lilun <a href="mailto:lil@chsi.com.cn">lilun</a>
 * @version $Id$ 2019年03月21 10:36
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserData userData = userService.getUserByUserName(userName);
        if (userData == null) {
            throw new UsernameNotFoundException("没有该用户");
        }
        return new UserDetailsImpl(userName, userData.getPassword(), userService.getRolesByUserId(userData.getId()));
    }
}
